home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Frameworks / Hsoi's App Shell 1.0a4 / HAS Other Source / WASTE 1.3a4 Distribution / WASTE 1.3a4 / Source / WEUtilities.c < prev   
Encoding:
C/C++ Source or Header  |  1996-12-30  |  3.9 KB  |  214 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WEUtilities.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Miscellaneous General-Purpose Utilities
  6.  *
  7.  *  Copyright (c) 1993-1997 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. pascal Boolean _WEBlockCmp
  18.     (
  19.         register const void * block1,
  20.         register const void * block2,
  21.         register Size blockSize
  22.     )
  23. {
  24.     for ( ; blockSize > 0 ; blockSize -- )
  25.     {
  26.         if ( * ( ( const UInt8 * ) block1 ) ++ != * ( ( const UInt8 * ) block2 ) ++ )
  27.         {
  28.             return false ;
  29.         }
  30.     }
  31.  
  32.     return true ;
  33. }
  34.  
  35. pascal void _WEBlockClr ( register void * block, register Size blockSize )
  36. {
  37.     register Size sixteen = 16 ;
  38.  
  39.     if ( blockSize >= sixteen )
  40.     {
  41.         //    advance block pointer to next even address
  42.         if ( ( UInt32 ) block & 1 )
  43.         {
  44.             * ( ( UInt8 * ) block ) ++ = 0 ;
  45.             blockSize -- ;
  46.         }
  47.  
  48.         //    advance block pointer to next address divisible by four
  49.         if ( ( UInt32 ) block & 2 )
  50.         {
  51.             * ( ( UInt16 * ) block ) ++ = 0 ;
  52.             blockSize -= 2 ;
  53.         }
  54.  
  55.         //    clear 16-byte blocks
  56.         for ( ; blockSize >= sixteen ; blockSize -= sixteen )
  57.         {
  58.             * ( ( UInt32 * ) block ) ++ = 0 ;
  59.             * ( ( UInt32 * ) block ) ++ = 0 ;
  60.             * ( ( UInt32 * ) block ) ++ = 0 ;
  61.             * ( ( UInt32 * ) block ) ++ = 0 ;
  62.         }
  63.     }
  64.  
  65.     //    clear remaining bytes
  66.     for ( ; blockSize > 0 ; blockSize -- )
  67.     {
  68.         * ( ( UInt8 * ) block ) ++ = 0 ;
  69.     }
  70. }
  71.  
  72. pascal void _WEForgetHandle ( Handle * h )
  73. {
  74.     Handle theHandle ;
  75.  
  76.     if ( ( theHandle = * h ) != nil )
  77.     {
  78.         * h = nil ;
  79.         DisposeHandle ( theHandle ) ;
  80.     }
  81. }
  82.  
  83. pascal Boolean _WESetHandleLock ( Handle h, Boolean lock )
  84. {
  85.     Boolean oldLock = ( HGetState ( h ) & ( 1 << 7 ) ) != 0 ;
  86.  
  87.     if ( lock != oldLock )
  88.     {
  89.         if ( lock )
  90.         {
  91.             HLock ( h ) ;
  92.         }
  93.         else
  94.         {
  95.             HUnlock ( h ) ;
  96.         }
  97.     }
  98.  
  99.     return oldLock ;
  100. }
  101.  
  102. pascal void _WEReorder ( SInt32 * a, SInt32 * b )
  103. {
  104.     if ( * a > * b )
  105.     {
  106.         SInt32 temp = * a ;
  107.         * a = * b ;
  108.         * b = temp ;
  109.     }
  110. }
  111.  
  112. pascal OSErr _WEAllocate ( Size blockSize, UInt32 allocFlags, Handle * h )
  113. {
  114.     // Allocate a new relocatable block.
  115.     // AllocFlags may specify whether the block should be cleared and whether
  116.     // temporary memory should be used.
  117.  
  118.     Handle theHandle = nil ;
  119.     OSErr err ;
  120.  
  121.     // if kAllocTemp is specified, try tapping temporary memory
  122.     if ( allocFlags & kAllocTemp )
  123.     {
  124.         theHandle = TempNewHandle ( blockSize, & err ) ;
  125.     }
  126.  
  127.     // if kAllocTemp isn't specified, or TempNewHandle failed, try with current heap
  128.     if ( theHandle == nil )
  129.     {
  130.         theHandle = NewHandle ( blockSize ) ;
  131.         err = MemError ( ) ;
  132.     }
  133.  
  134.     // if kAllocClear is specified, zero the block
  135.     if ( ( allocFlags & kAllocClear ) && ( theHandle != nil ) )
  136.     {
  137.         _WEBlockClr ( * theHandle, blockSize ) ;
  138.     }
  139.  
  140.     * h = theHandle ;
  141.     return err ;
  142. }
  143.  
  144. pascal OSErr _WESplice ( Handle h, const void * blockPtr, SInt32 blockSize, SInt32 offset )
  145. {
  146.     SInt32 oldSize, newSize ;
  147.     OSErr err ;
  148.  
  149.     //    get handle size
  150.     oldSize = GetHandleSize ( h ) ;
  151.  
  152.     //    calculate new size
  153.     newSize = oldSize + blockSize ;
  154.  
  155.     if ( blockSize < 0 )
  156.     {
  157.         //    negative blockSize means we must remove a portion of the block
  158.         //    offset == -1 means remove trailing portion
  159.         if ( offset == -1 )
  160.         {
  161.             offset = newSize ;
  162.         }
  163.  
  164.         //    sanity checks
  165.         err = paramErr ;
  166.         if ( ( offset < 0 ) || ( offset > newSize ) )
  167.         {
  168.             goto cleanup ;
  169.         }
  170.  
  171.         //    compact the handle
  172.         BlockMoveData ( * h + ( offset - blockSize ), * h + offset, newSize - offset ) ;
  173.     }
  174.  
  175.     //    resize the handle
  176.     SetHandleSize ( h, newSize ) ;
  177.     if ( ( err = MemError ( ) ) != noErr )
  178.     {
  179.         goto cleanup ;
  180.     }
  181.  
  182.     if ( blockSize > 0 )
  183.     {
  184.         //    positive blockSize means make room within the handle
  185.         //    offset == -1 means append new portion at the end
  186.         if ( offset == -1 )
  187.         {
  188.             offset = oldSize ;
  189.         }
  190.  
  191.         //    sanity checks
  192.         err = paramErr ;
  193.         if ( ( offset < 0 ) || ( offset > oldSize ) )
  194.         {
  195.             goto cleanup ;
  196.         }
  197.  
  198.         //    make some room at offset
  199.         BlockMoveData ( * h + offset, * h + offset + blockSize, oldSize - offset ) ;
  200.  
  201.         //    insert blockPtr into gap (unless blockPtr == nil)
  202.         if ( blockPtr != nil )
  203.         {
  204.             BlockMoveData ( blockPtr, * h + offset, blockSize ) ;
  205.         }
  206.     }
  207.  
  208.     //    clear result code
  209.     err = noErr ;
  210.  
  211. cleanup :
  212.     return err ;
  213. }
  214.